package com.harry.resource.inner.module.srv;

import com.harry.resource.inner.entity.InnerUser;

import org.apache.commons.codec.binary.StringUtils;

import com.harry.decoupling.impl.module.GenericSrvImpl;
import com.harry.utility.PswdUtils;

public class InnerUserRestfulService extends GenericSrvImpl<InnerUser> {

	@Override
	public InnerUser save(InnerUser t) throws Exception {
		InnerUser dbUser;
		if (t.getId() == null) {
			t.setPassword(PswdUtils.encrypt(t.getPassword()));
		} else if (null == (dbUser = entityDao().expandEntityById(t.getId()))) {
			throw new Exception("User[id=" + t.getId() + "] does not exists");
		} else if (StringUtils.equals(t.getPassword(), dbUser.getPassword())) {
			// Password does not changed.
		} else {
			t.setPassword(PswdUtils.encrypt(t.getPassword()));
		}
		return super.save(t);
	}

	@Override
	public String getNameFieldName() {
		return "username";
	}
}
